﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Windows;
using BrainTechLLC;
using BrainTechLLC.FolderExplorer;
using BrainTechLLC.WPF;
using DirectoryViewModels;
using System.Windows.Controls;
using System.Windows.Forms;

namespace FolderView.Plugin
{
    public class FolderExplorerWindowHandler : WindowHandlerBase<FolderExplorerWindow, Window>, IFolderExplorerWindowHandler
    {
        public new event EventHandler ClosedEvent;
        public event EventHandler SelectFoldersStart;
        public event EventHandler SelectFoldersEnd;
        public event EventHandler<ObjectEventArgs<object, ItemsControl, UIElement>> DropData;
        protected DirectoriesToExamineViewModel _DirectoriesToExamineViewModel;
        public static PluginDefinitionLoaded<IFolderExplorerWindowHandler> Handler;
        protected bool _AlwaysOnTop;

        public bool AlwaysOnTop
        {
            get { return _AlwaysOnTop; }
            set
            {
                if (_AlwaysOnTop != value && _window != null)
                {
                    _window.AlwaysOnTop = value;
                }

                _AlwaysOnTop = value;
            }
        }

        public DirectoriesToExamineViewModel DirectoriesToExamineViewModel
        {
            get { return _DirectoriesToExamineViewModel; }
            set { _DirectoriesToExamineViewModel = value; }
        }

        public FolderExplorerWindowHandler(Window parentWindow, PluginDefinitionLoaded<IFolderExplorerWindowHandler> handler)
            : base(parentWindow)
        {
            Handler = handler;
            _parentWindow = parentWindow;
        }

        public void RaiseDropData(object o1, ItemsControl o2, UIElement o3)
        {
            DropData.RaiseEvent(this, o1, o2, o3);
        }

        public override void OpenWindow(Window owner)
        {
            if (_window == null)
            {
                _window = new FolderExplorerWindow()
                {
                    Owner = owner,
                    Left = Screen.PrimaryScreen.WorkingArea.Width - ((double)Screen.PrimaryScreen.WorkingArea.Width / 3D),
                    Top = 0,
                    Width = (double)Screen.PrimaryScreen.WorkingArea.Width / 3D,
                    Height = (double)Screen.PrimaryScreen.WorkingArea.Height,
                    WindowStartupLocation = WindowStartupLocation.Manual,
                };
                _window.KeyDown += new System.Windows.Input.KeyEventHandler(_window_KeyDown);
                _window.Closed += new EventHandler(_window_Closed);
                _window.Closing += new System.ComponentModel.CancelEventHandler(_window_Closing);
                _window.SelectFoldersStart += new EventHandler<ObjectEventArgs<FolderViewModel>>(_window_SelectFoldersStart);
                _window.SelectFoldersEnd += new EventHandler<ObjectEventArgs<FolderViewModel>>(_window_SelectFoldersEnd);
                _window.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                _window.VerticalAlignment = VerticalAlignment.Stretch;

                if (Settings.Values.FolderExplorerWindowState == WindowState.Maximized)
                {
                    _window.WindowState = WindowState.Maximized;
                }
                else if (Settings.Values.FoldersViewLocation != null && Settings.Values.FolderExplorerWindowState != WindowState.Minimized)
                {
                    if (Settings.Values.FoldersViewLocation.Width == 0)
                    {
                        _window.WindowState = WindowState.Normal;
                    }
                    else
                    {
                        _window.Left = Settings.Values.FoldersViewLocation.Left;
                        _window.Top = Settings.Values.FoldersViewLocation.Top;
                        _window.Width = Settings.Values.FoldersViewLocation.Width;
                        _window.Height = Settings.Values.FoldersViewLocation.Height;
                    }
                }


                _window.Show();
            }
            else
            {
                _window.Show();
            }

            _window.Focus();
            _window.AlwaysOnTop = AlwaysOnTop;
            _window.folderExplorer.RootFolder = new FolderViewModel(Handler.Handler.DirectoriesToExamineViewModel, null) { FullPath = @"C:\" };
            _window.WireUpSizeAndLocationChanged();
        }

        void _window_SelectFoldersEnd(object sender, ObjectEventArgs<FolderViewModel> e)
        {
            SelectFoldersEnd.RaiseEvent(this);
        }

        void _window_SelectFoldersStart(object sender, ObjectEventArgs<FolderViewModel> e)
        {
            SelectFoldersStart.RaiseEvent(this);
        }

        void _window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_window != null && _window.IsVisible)
            {
                _window.SaveSizeAndLocation();
            }
        }

        void _window_Closed(object sender, EventArgs e)
        {
            if (_window != null)
            {
                _window.KeyDown -= _window_KeyDown;
                _window.Closed -= _window_Closed;
                _window.Closing -= _window_Closing;
                _window.SelectFoldersStart -= _window_SelectFoldersStart;
                _window.SelectFoldersEnd -= _window_SelectFoldersEnd;

                _window = null;
            }
        }

        void _window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
            {
                _window.Close();
                ClosedEvent.RaiseEvent(this);
            }
        }
    }
}
